package org.com.android.worker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.com.android.constant.Constant;
import org.com.android.db.Good;
import org.com.android.db.Itable;
import org.com.android.db.Schema;
import org.com.android.network.NetworkClient;
import org.com.android.polo.main;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class Worker extends Thread {

	private Handler handler;
	private Object data;
	private NetworkClient client;
	private Schema schema;
	private int action;
	private String target ;

	public Worker(Handler handler, NetworkClient client ,Object data,
			Schema schema, int action, String target) {
		this.handler = handler;
		this.data = data;
		this.client = client;
		this.schema = schema;
		this.action = action;
		this.target = target ;
	}

	@Override
	public void run() {
		Message msg = handler.obtainMessage();
		msg.what = action;
		int action_result = Constant.POLO_ACTION_DEFAULT ;
		switch (action) {
		case Constant.POLO_ACTION_SUBMIT:{
			action_result = Constant.POLO_ACTION_SUBMIT_FAILURE;
			Map<String, String> d = (Map<String, String>) data;
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("entity", d.get("entity"));
			params.put("id", d.get("barcode"));

			String resp = client.post(this.target,params);
			try {
				final JSONObject data = new JSONObject(resp);
				JSONObject result = data.getJSONObject("data");
				if (result.getString("result").compareTo("success") == 0) {
					HashMap<String, String> item = new HashMap<String, String>();
					item.put("_id", d.get("barcode"));
					item.put("status", String.valueOf(Good.Status.submitted));
					schema.gettable(Good.name).update(item);
					action_result = Constant.POLO_ACTION_SUBMIT_SUCCESS;
				} else {
					Log.e("main::onsubmit-failure", result.getString("result"));
				}
			} catch (JSONException e) {
				Log.e("worker::run", e.getMessage());
			}
		}
		break;
		case Constant.POLO_ACTION_SYNC:{
			action_result = Constant.POLO_ACTION_SYNC_FAILURE ;
			Map<String, String> params = (Map<String,String>)data ;
			Log.d("run lastupdated", params.get("lastupdated"));
			String resp = client.post(target,params);
			if (resp != null) {
				try {
					final JSONObject data = new JSONObject(resp);
					JSONArray ids = data.getJSONArray("data");
					Itable good = schema.gettable(Good.name);
					for (int i = 0; i < ids.length(); i++) {
						Map<String, String> map = new HashMap<String, String>();
						map.put("_id", ids.getJSONObject(i).getString("id"));
						map.put("status", "0");
						Log.d("_id", ids.getJSONObject(i).getString("id"));
						good.insert(map) ;
					}
					action_result = Constant.POLO_ACTION_SYNC_SUCCESS;
				} catch (JSONException e) {
					Log.e("submit", e.getMessage());
				}
			}
		}	
		break;
		}
		msg.getData().putInt("result", action_result);
		handler.sendMessage(msg);
	}
}